<template>
  <el-dialog v-if="load" :close-on-click-modal="false" :title="form.ad_id ? '编辑授权接口' : '添加授权接口'" v-model="visible" append-to-body width="800px" @close="handleClose">
    <el-form ref="formRef" :model="form" label-position="top">
      <el-form-item label="平台" prop="service_code" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
        Amazom
      </el-form-item>
      <el-form-item label="名称" prop="name" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
        <el-input v-model="form.name" placeholder="请输入" class="w-full" />
      </el-form-item>
      <el-form-item label="Lwa Client Id" prop="lwa_client_id" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
        <el-input v-model="form.lwa_client_id" placeholder="请输入" class="w-full" />
      </el-form-item>
      <el-form-item label="Lwa Client Secret" prop="lwa_client_secret" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
        <el-input v-model="form.lwa_client_secret" placeholder="请输入" class="w-full" />
      </el-form-item>
      <el-form-item label="Aws Access Key Id" prop="aws_access_key_id" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
        <el-input v-model="form.aws_access_key_id" placeholder="请输入" class="w-full" />
      </el-form-item>
      <el-form-item label="Aws Secret Access Key" prop="aws_secret_access_key" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
        <el-input v-model="form.aws_secret_access_key" placeholder="请输入" class="w-full" />
      </el-form-item>
      <el-form-item label="Role Arn" prop="role_arn" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
        <el-input v-model="form.role_arn" placeholder="请输入" class="w-full" />
      </el-form-item>
      <el-form-item label="Application Id" prop="application_id" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
        <el-input v-model="form.application_id" placeholder="请输入" class="w-full" />
      </el-form-item>
      <el-form-item label="状态">
        <el-switch v-model="form.status" inline-prompt active-text="启用" inactive-text="禁用" :active-value='1'
          :inactive-value='2' />
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="form.remark" type="textarea" :autosize="{ minRows: 2, maxRows: 5 }" placeholder="请输入" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">关 闭</el-button>
      <el-button type="primary" :loading="loadings.submit" @click="submit">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import api from '../../api'
import { ElMessage } from 'element-plus'

const load = ref(false)
const visible = ref(false)
let loadings = reactive({})
const form = ref({})
const formRef = ref()
const service_code = 'amazon'

const emit = defineEmits(['close', 'success'])

// 打开弹窗
const open = async (ad_id) => {
  if (!load.value) {
    load.value = true
    await 1
  }
  loadings = reactive({})
  visible.value = true
  if (ad_id) {
    const res = await api.detail({ ad_id, service_code })
    form.value = res.data
    form.value.service_code = service_code
    formRef?.value?.clearValidate()
  } else {
    form.value = { service_code, status: 1 }
    formRef?.value?.resetFields()
  }
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const submit = async() => {
  const validator = await formRef?.value.validate()
  if (!validator) return
  loadings.submit = true
  api[form.value.ad_id ? 'edit' : 'create'](form.value).then(res => {
    ElMessage.success('操作成功')
    emit('success')
    close()
  }).finally(() => {
    loadings.submit = false
  })
}

defineExpose({ open })
</script>